<html>
<head><title>CS143 Project 1B</title></head>
<body>
<p>Type an SQL query in the following box:</p>
<p>
<form method='GET'>
<textarea name="query" cols="60" rows="8"></textarea>
<input type="submit" value="Submit" />
</form>
</p>
<p><small>Tables and Fields are case sensitive. To see a list of available tables, run "show tables"</small></p>

<?php
$query = $_GET['query'];
if($query == NULL)
  exit();
$conn = mysql_connect("localhost", "cs143", "");

if(!$conn) {
$errmsg = mysql_error($conn);
print "The SQL connection has failed: $errmsg <br />:";
exit(1);
}
mysql_select_db("CS143", $conn);
$result = mysql_query($query);
if(!$result) {
  $message = 'Invalid query: ' . mysql_error() . "\n";
  die($message);
}
$numcol = mysql_num_fields($result);
$numrow = mysql_num_rows($result);
echo '<table border=1 cellspacing=1 cellpadding=2>';
echo '<tr align = center>';
for($i=0; $i < $numcol; $i++){
  echo '<td><b>';
  $name = mysql_field_name($result, $i);
  echo $name;
  echo '</b></td>';
}
  
while($row = mysql_fetch_array($result)) {
  echo '<tr align = center>';
  for($i=0; $i < $numcol; $i++){
    echo '<td>';
	if($row[$i] == NULL)
	  echo 'N/A';
	else
      echo $row[$i];
	echo '</td>';
  }
  echo '</tr>';
}

mysql_close($conn);
?>
</body>
</html>




